/*
 * Powered By 
 * Since 2010 - 2016
 */

package com.rf.business.control;

import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.rf.sys.frm.BaseController;
import com.rf.sys.utils.ConfigUtils;
import com.rf.sys.utils.JsonUtils;
import com.rf.sys.utils.PageUtils;
import com.rf.sys.utils.UuidUtil;
import com.rf.business.model.AutoRelease;
import com.rf.business.service.AutoReleaseService;

@Controller
@RequestMapping("/bus/autoRelease")
public class AutoReleaseController extends BaseController{
	/**
	 * 日志
	 */
	private static Logger log = LoggerFactory.getLogger(AutoReleaseController.class);
	private static final long serialVersionUID = -120161117110144106L;
	
	@Resource
	private AutoReleaseService autoReleaseService;
	
	/**
	 * 初始化页面信息
	 * @param request,response
	 * @return ModelAndView
	 * @throws ServletException,IOException
	 */
	@RequestMapping(value = "index")
    public ModelAndView index (HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView modelAndView = new ModelAndView();
		String type = request.getParameter("type");
		if(type.equals("bjrj")){
			modelAndView.setViewName("/business/autoRelease/bjrj_list");
		}else if(type.equals("bjcj")){
			modelAndView.setViewName("/business/autoRelease/bjcj_list");
		}else if(type.equals("gjcj")){
			modelAndView.setViewName("/business/autoRelease/gjcj_list");
		}else if(type.equals("gjrj")){
			modelAndView.setViewName("/business/autoRelease/gjrj_list");
		}
		
		return modelAndView; 
    }
    
    /**
	 * 搜索页面信息
	 * @param Page,request,response
	 * @return JSON
	 * @throws ServletException,IOException
	 */
    @RequestMapping(value = "query")
    @ResponseBody
    public void query (PageUtils pageUtils, AutoRelease autoRelease, HttpServletRequest request, HttpServletResponse response ) throws Exception{
    	//获取分页、查询参数，进行数据封装
    	Map searchMap = autoReleaseService.getAutoReleasePage(autoRelease, pageUtils);
    	super.responsePrint(request, response, JsonUtils.mapToJson(searchMap));
    }
    
    /**
     * 编辑页面初始化
     * @param request,response
	 * @return ModelAndView
	 * @throws ServletException,IOException
     */
    @RequestMapping(value = "edit")
    public ModelAndView edit (HttpServletRequest request, HttpServletResponse response ) throws Exception{
    	ModelAndView modelAndView = new ModelAndView();
		String id = request.getParameter("id");
		String type = request.getParameter("type");
		AutoRelease autoRelease = new AutoRelease();
		if(id != null && !id.equals("")){
			autoRelease = autoReleaseService.getAutoReleaseByID(id);	
		}else{
//			autoRelease.setState(ConfigUtils.ENABLESTATE);
		}
		modelAndView.addObject("autoRelease", autoRelease);
		if(type.equals("bjrj")){
			modelAndView.setViewName("/business/autoRelease/bjrj_edit");
		}else if(type.equals("bjcj")){
			modelAndView.setViewName("/business/autoRelease/bjcj_edit");
		}else if(type.equals("gjcj")){
			modelAndView.setViewName("/business/autoRelease/gjcj_edit");
		}else if(type.equals("gjrj")){
			modelAndView.setViewName("/business/autoRelease/gjrj_edit");
		}
        return modelAndView; 
    }
    
    /**
     * 保存数据信息
     * @param AutoRelease,request,response
	 * @return ModelAndView
	 * @throws ServletException,IOException
     */
    @RequestMapping(value = "save")
    public void save (AutoRelease autoRelease, HttpServletRequest request, HttpServletResponse response ) throws Exception{
    	String ret = "success";
    	if(autoRelease !=null && autoRelease.getId()!=null && !autoRelease.getId().equals("")){
			//修改
			autoReleaseService.updateAutoRelease(autoRelease);
		}else{
			//新增
			autoRelease.setId(UuidUtil.get32UUID());
			autoReleaseService.addAutoRelease(autoRelease);
		}
		super.resPrint(request, response, ret);//成功 
    }
    
    /**
     * 删除数据信息
     * @param request,response
	 * @return JSON
	 * @throws ServletException,IOException
	 */
    @RequestMapping(value = "delete")
    @ResponseBody
    public void delete (HttpServletRequest request, HttpServletResponse response) throws Exception{
    	JSONObject returnObj = new JSONObject();
    	String ids = request.getParameter("ids");
    	String [] idsArr = ids.split(",");
    	for(int i=0;i<idsArr.length;i++){
    		autoReleaseService.deleteAutoRelease(idsArr[i]);
    	}
		returnObj.element("message", "success");//成功
		super.responseJson(request, response, returnObj.toString());
    }
    
    /**
     * 更新数据状态
     * @param autoRelease,request,response
	 * @return JSON
	 * @throws ServletException,IOException
	 */
    @RequestMapping(value = "updateState")
    @ResponseBody
    public void updateState (AutoRelease autoRelease, HttpServletRequest request, HttpServletResponse response) throws Exception{
    	JSONObject returnObj = new JSONObject();
    	autoReleaseService.updateAutoRelease(autoRelease);
		returnObj.element("message", "success");//成功
		super.responseJson(request, response, returnObj.toString());
    }
}


